package com.zhkj.txg.module.find.ui

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.R
import com.zhkj.txg.module.find.adapter.AssociateProductAdapter
import com.zhkj.txg.module.find.entity.AssociateProductEntity
import com.zhkj.txg.module.find.vm.FindViewModel
import com.zhkj.txg.utils.TextWatcherWrapper
import kotlinx.android.synthetic.main.activity_find_article_product.*

class FindArticleProductActivity : ViewModelActivity<FindViewModel>() {
    override fun setViewModel(): FindViewModel = createViewModel()
    override fun statusTitleBar(): View? = titlePublishProduct
    override val layoutResId: Int = R.layout.activity_find_article_product

    companion object {
        fun selectProduct(activity: Activity, requestCode: Int) {
            val intent = Intent(activity, FindArticleProductActivity::class.java)
            activity.startActivityForResult(intent, requestCode)
        }
    }

    private val productAdapter = AssociateProductAdapter()
        .apply {
            loadMoreModule?.isEnableLoadMore = true
            loadMoreModule?.isEnableLoadMoreIfNotFullPage = true
            loadMoreModule?.setOnLoadMoreListener {
                viewModel.associateProductList(
                    AccountManager.getToken()
                )
            }
            setOnItemClickListener { _, _, position ->
                if (data[position].itemType == AssociateProductEntity.TYPE_PRODUCT) {
                    intent.putExtra("data", data[position])
                }
                setResult(Activity.RESULT_OK, intent)
                finish()
            }
        }
    private val searchProductAdapter = AssociateProductAdapter()
        .apply {
            loadMoreModule?.isEnableLoadMore = true
            loadMoreModule?.isEnableLoadMoreIfNotFullPage = true
            loadMoreModule?.setOnLoadMoreListener {
                viewModel.searchAssociateProduct(
                    AccountManager.getToken(),
                    etArticlePublishSearch.text?.toString() ?: ""
                )
            }
            setOnItemClickListener { _, _, position ->
                intent.putExtra("data", data[position])
                setResult(Activity.RESULT_OK, intent)
                finish()
            }
        }


    override fun initViewModel() {
        viewModel.productListLiveData.observe(this, Observer { it ->
            when (it.status) {
                HttpResponseStatus.Status.START -> Unit
                HttpResponseStatus.Status.SUCCESS -> it.response?.let {
                    // 数据填充
                    if (it.isLoadMore) productAdapter.addData(it.data)
                    else productAdapter.setNewData(it.data)
                    // 判断加载状态
                    if (it.hasMore) productAdapter.loadMoreModule?.loadMoreComplete()
                    else productAdapter.loadMoreModule?.loadMoreEnd()
                }
                HttpResponseStatus.Status.FAILURE -> {
                    it.exception?.printStackTrace()
                    toast(it.statusTip)
                    productAdapter.loadMoreModule?.loadMoreFail()
                }
            }
        })
        viewModel.searchProductLiveData.observe(this, Observer { it ->
            when (it.status) {
                HttpResponseStatus.Status.START -> Unit
                HttpResponseStatus.Status.SUCCESS -> it.response?.let {
                    rvArticlePublishSearch.visibility = View.VISIBLE
                    // 数据填充
                    if (it.isLoadMore) searchProductAdapter.addData(it.data)
                    else searchProductAdapter.setNewData(it.data)
                    // 判断加载状态
                    if (it.hasMore) searchProductAdapter.loadMoreModule?.loadMoreComplete()
                    else searchProductAdapter.loadMoreModule?.loadMoreEnd()
                }
                HttpResponseStatus.Status.FAILURE -> {
                    it.exception?.printStackTrace()
                    toast(it.statusTip)
                    searchProductAdapter.loadMoreModule?.loadMoreFail()
                }
            }
        })
    }

    override fun initUI() {
        // 搜索按钮点击监听
        tvArticlePublishSearch.setOnClickListener {
            viewModel.searchAssociateProduct(
                AccountManager.getToken(),
                etArticlePublishSearch.text?.toString() ?: "", true
            )
        }
        // 输入框字数监听
        etArticlePublishSearch.addTextChangedListener(TextWatcherWrapper {
            if (it.isNullOrBlank()) rvArticlePublishSearch.visibility = View.GONE
        })
        with(rvArticlePublishProduct) {
            layoutManager = LinearLayoutManager(this@FindArticleProductActivity)
            adapter = productAdapter
            productAdapter.setEmptyView(R.layout.layout_empty_data)
        }
        with(rvArticlePublishSearch) {
            layoutManager = LinearLayoutManager(this@FindArticleProductActivity)
            adapter = searchProductAdapter
            searchProductAdapter.setEmptyView(R.layout.layout_empty_data)
        }
    }

    override fun initData() {
        viewModel.associateProductList(
            AccountManager.getToken()
        )
    }
}